﻿using System;
using System.Collections.Generic;
using System.Web;

/// <summary>
///公司类别
/// </summary>
public class clsCompanyClass
{
	public clsCompanyClass()
	{
	}

    public clsCompanyClass(string name,int code)
    {
        this._companyclassname = name;
        this._companyclasscode = code;
    }

    private string _companyclassname = "";
    /// <summary>
    /// 公司类别名
    /// </summary>
    public string CompanyClassName
    {
        get { return _companyclassname; }
        set { _companyclassname = value; }
    }

    private int _companyclasscode = 0;
    /// <summary>
    /// 公司类别代码
    /// </summary>
    public int CompanyClassCode
    {
        get { return _companyclasscode; }
        set { _companyclasscode = value; }
    }

    private static List<clsCompanyClass> _classlist;

    public static List<clsCompanyClass> GetClass()
    {
        //单例模式？？
        if (_classlist == null)
        {
            _classlist = new List<clsCompanyClass>();
            _classlist.Add(new clsCompanyClass("广告促销", 1));
            _classlist.Add(new clsCompanyClass("数码电子", 2));
            _classlist.Add(new clsCompanyClass("茶具杯壶", 4));
            _classlist.Add(new clsCompanyClass("家居生活", 8));
            _classlist.Add(new clsCompanyClass("生活电器", 16));
            _classlist.Add(new clsCompanyClass("五金工具", 32));
            _classlist.Add(new clsCompanyClass("商务办公", 64));
            _classlist.Add(new clsCompanyClass("箱包皮具", 128));
            _classlist.Add(new clsCompanyClass("运动户外", 256));
            _classlist.Add(new clsCompanyClass("工艺收藏", 512));
            _classlist.Add(new clsCompanyClass("食品/特产", 1024));
            _classlist.Add(new clsCompanyClass("印刷包装", 2048));
        }
        return _classlist;
    }

    public static string GetClassNameByCode(int code)
    {
        List<clsCompanyClass> ls = GetClass();
        string name = "";

        foreach(clsCompanyClass cc in ls)
        {
            if (cc._companyclasscode == code)
            {
                name = cc._companyclassname;
                break;
            }
        }

        return name;
    }

    /// <summary>
    /// 名称转代码
    /// </summary>
    /// <param name="p">格式如：广告促销,数码电子,生活电器</param>
    /// <returns></returns>
    public static int NameToCode(string p)
    {
        int ret=0;
        List<clsCompanyClass> ls = GetClass();
        
        string[] arrP = p.Split(',');
        int len = arrP.Length;

        for (int i = 0; i < len; i++)
        {
            string name = arrP[i].Trim();
            foreach (clsCompanyClass cc in ls)
            {
                if (name != "" && name == cc._companyclassname)
                    ret |= cc._companyclasscode;
            }
        }
        return ret;
    }

    /// <summary>
    /// 代码转名称
    /// </summary>
    /// <param name="code"></param>
    /// <returns>格式如：广告促销,数码电子,生活电器</returns>
    public static string CodeToName(int code)
    {
        string ret = "";
        List<clsCompanyClass> ls = GetClass();

        foreach (clsCompanyClass cc in ls)
        {
            if ((code & cc._companyclasscode) == cc._companyclasscode)
            {
                if (ret == "")
                    ret = cc._companyclassname;
                else
                    ret += "," + cc._companyclassname;
            }
        }
        return ret;
    }

}